package com.eascs.crm.nsEditDetail.controller;

import static com.eascs.site.view.HtmlRestResult.popHtmlFragment;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.admin.o2o.loan.entity.O2oLoanapplyEntity;
import com.eascs.admin.o2o.loan.service.O2oLoanapplyService;
import com.eascs.app.log.Logger;
import com.eascs.app.log.LoggerFactory;
import com.eascs.common.util.StringUtil;
import com.eascs.crm.nsEditDetail.entity.CustomerShopInfoEntity;
import com.eascs.crm.nsEditDetail.entity.CustomerShopInfoEntityNew;
import com.eascs.crm.nsEditDetail.service.CustomerShopInfoService;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.view.BaseRestResult;
import com.eascs.site.view.HtmlRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;

/**
 * 内审信息编辑
 * @author yy
 *
 */
@Controller
@RequestMapping("/customer/shop/rest")
public class CustomerShopInfoRestController extends BaseRestController {
	private static Logger logger = LoggerFactory.getLogger(CustomerShopInfoRestController.class);
	private static String searchCustomerNameUrl = "/common/searchCustomerName?shopcode=";

	@Autowired
	CustomerShopInfoService shopInfoService;
	@Autowired
	O2oLoanapplyService  o2oLoanapplyService;
	@Value("${web.crm.url}")
	private String crmUrl;

	@RequestMapping("/addPage")
	public HtmlRestResult customerShopInfoAddPage(String loanapplyNo) {
		CustomerShopInfoEntity info = new CustomerShopInfoEntity();
		info.setLoanapplyNo(loanapplyNo);
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		
//		info.setShopno(info.getShopno()+","+info.getShopname());
		info.setShopno(info.getShopno());

		ViewContext.put("view", info);
		ViewContext.put("o2oLoanapplyEntity", o2oLoanapplyEntity);
		ViewContext.put("crmUrl", crmUrl);
		ViewContext.put("searchCustomerNameUrl", crmUrl + searchCustomerNameUrl);
		return popHtmlFragment("/nsEditDetail/customer_shop_info_add");
	}
	@RequestMapping("/addPagenew")
	public HtmlRestResult customerShopInfoAddPagenew(String loanapplyNo) {
//		CustomerShopInfoEntity info = new CustomerShopInfoEntity();
//		info.setLoanapplyNo(loanapplyNo);
		O2oLoanapplyEntity o2oLoanapplyEntity = o2oLoanapplyService.findById(loanapplyNo);
		
//		info.setShopno(info.getShopno()+","+info.getShopname());
//		info.setShopno(info.getShopno());
//
//		ViewContext.put("view", info);
		ViewContext.put("o2oLoanapplyEntity", o2oLoanapplyEntity);
		ViewContext.put("crmUrl", crmUrl);
		ViewContext.put("searchCustomerNameUrl", crmUrl + searchCustomerNameUrl);
		return popHtmlFragment("/nsEditDetail/customer_shop_info_new_add");
	}
	@RequestMapping("/add")
	public BaseRestResult<String> customerShopInfoAdd(CustomerShopInfoEntity info) {
		createCustomerShopInfo(info);
		
		if(StringUtil.isBlank(info.getShopname())){
			return BaseRestResult.warn("请在下拉框中选择sd客户");
		}
		
		if(StringUtil.isBlank(info.getShopcode())){
			return BaseRestResult.warn("请在下拉框中选择sd客户");
		}
		
		boolean flag = shopInfoService.insert(info);
		if(flag) {
			return BaseRestResult.info("添加门店信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("添加门店信息失败").andRefresh();
		}
		//return HtmlRestResult.popHtmlFragment("/nsEditDetail/customer_shop_info_add");
	}
	@RequestMapping("/addnew")
	public BaseRestResult<String> customerShopInfoAddnew(CustomerShopInfoEntityNew info) {
		
		User user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());
		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());
		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
		boolean flag = shopInfoService.insertnew(info);
		if(flag) {
			return BaseRestResult.info("添加门店信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("添加门店信息失败").andRefresh();
		}
		//return HtmlRestResult.popHtmlFragment("/nsEditDetail/customer_shop_info_add");
	}
	@RequestMapping("/updatePage")
	public HtmlRestResult customerShopInfoUpdatePage(String customerShopId) {
		CustomerShopInfoEntity info = shopInfoService.queryById(customerShopId);
		O2oLoanapplyEntity o2oLoanapplyEntity = null;
		if (null != info) {
			o2oLoanapplyEntity = o2oLoanapplyService.findById(info.getLoanapplyNo());
		}
//		info.setShopno(info.getShopno()+","+info.getShopname());
		info.setShopno(info.getShopno());
		ViewContext.put("o2oLoanapplyEntity", o2oLoanapplyEntity);
		ViewContext.put("view", info);
		ViewContext.put("crmUrl", crmUrl);
		ViewContext.put("searchCustomerNameUrl", crmUrl + searchCustomerNameUrl);
		return popHtmlFragment("/nsEditDetail/customer_shop_info_update");
	}
	@RequestMapping("/updatePagenew")
	public HtmlRestResult customerShopInfoUpdatePagenew(String customerShopId) {
		CustomerShopInfoEntityNew info = shopInfoService.queryByIdnew(customerShopId);
		O2oLoanapplyEntity o2oLoanapplyEntity = null;
		if (null != info) {
			o2oLoanapplyEntity = o2oLoanapplyService.findById(info.getLoanapplyNo());
		}
		ViewContext.put("o2oLoanapplyEntity", o2oLoanapplyEntity);
		ViewContext.put("view", info);
		ViewContext.put("crmUrl", crmUrl);
		ViewContext.put("searchCustomerNameUrl", crmUrl + searchCustomerNameUrl);
		return popHtmlFragment("/nsEditDetail/customer_shop_info_new_update");
	}
	/**
	 * @Description:
	 *               <p>
	 *               门店显示详情
	 *               </p>
	 * @author CXW
	 * @date 2017年7月25日 下午7:45:43
	 * @version V1.0
	 */
	@RequestMapping("/detailPage")
	public HtmlRestResult customerShopInfoDetail(String customerShopId, String type, String approvalStatus) {
		logger.info("门店查询id：【{}】", customerShopId);
		CustomerShopInfoEntity info = shopInfoService.queryById(customerShopId);
		info.setShopno(info.getShopno()+","+info.getShopname());
		ViewContext.put("type", type);
		ViewContext.put("approvalStatus", approvalStatus);
		ViewContext.put("view", info);
		return popHtmlFragment("/nsEditDetail/customer_shop_info_detail");
	}
	@RequestMapping("/detailPagenew")
	public HtmlRestResult customerShopInfoDetailnew(String customerShopId, String type, String approvalStatus) {
		logger.info("门店查询id：【{}】", customerShopId);
		CustomerShopInfoEntityNew info = shopInfoService.queryByIdnew(customerShopId);
		ViewContext.put("view", info);
		ViewContext.put("type", type);
		ViewContext.put("approvalStatus", approvalStatus);
		return popHtmlFragment("/nsEditDetail/customer_shop_info_new_detail");
	}
	@RequestMapping("/update")
	public BaseRestResult<String> customerShopInfoUpdate(CustomerShopInfoEntity info) {
		if(StringUtil.isBlank(info.getCustomerShopId())){
			return BaseRestResult.warn("更新门店信息失败:记录不存在").andRefresh();
		}

		if(StringUtil.isBlank(info.getShopname())){
			return BaseRestResult.warn("请在下拉框中选择sd客户");
		}
		
		if(StringUtil.isBlank(info.getShopcode())){
			return BaseRestResult.warn("请在下拉框中选择sd客户");
		}
		
		modifyCustomerShopInfo(info);
		boolean flag = shopInfoService.update(info);
		if(flag) {
			return BaseRestResult.info("修改门店信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("修改门店信息失败").andRefresh();
		}
	}
	
	@RequestMapping("/updatenew")
	public BaseRestResult<String> customerShopInfoUpdatenew(CustomerShopInfoEntityNew info) {
		if(StringUtil.isBlank(info.getCustomerShopId())){
			return BaseRestResult.warn("更新门店信息失败:记录不存在").andRefresh();
		}
		User user = UserHolder.getUser();
		info.setModifyDate(new Date());
		info.setModifyId(user.getNo());
		info.setModifier(user.getCname());
		boolean flag = shopInfoService.updatenew(info);
		if(flag) {
			return BaseRestResult.info("修改门店信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("修改门店信息失败").andRefresh();
		}
	}
	@RequestMapping("/del")
	public BaseRestResult<String> customerShopInfoDel(String customerShopId) {
		boolean flag = shopInfoService.delete(customerShopId);
		if(flag) {
			return BaseRestResult.info("删除门店信息成功").andRefresh();
		} else {
			return BaseRestResult.warn("删除门店信息失败").andRefresh();
		}
	}

	private void createCustomerShopInfo(CustomerShopInfoEntity info){
		User user = UserHolder.getUser();
		info.setCreateDate(new Date());
		info.setModifyDate(new Date());
		info.setCreateId(user.getNo());
		info.setModifyId(user.getNo());
		info.setCreator(user.getCname());
		info.setModifier(user.getCname());
	}

	private void modifyCustomerShopInfo(CustomerShopInfoEntity info){
		User user = UserHolder.getUser();
		info.setModifyDate(new Date());
		info.setModifyId(user.getNo());
		info.setModifier(user.getCname());
	}
	
	/**
	 *  CXW 贷款详情显示证件号码等信息星号 http://tick.o2ojr.eascs.com/ticket/view/loan-12592
	 * @param houseId
	 * @return
	 */
	@RequestMapping("/view_info")
	public BaseRestResult<String> view_info(String customerShopId) {
		CustomerShopInfoEntity info = shopInfoService.queryById(customerShopId);
		ViewContext.put("view", info);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/view_info_shop");
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               新门店信息--详情脱敏
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/view_info_new")
	public BaseRestResult<String> view_info_new(String customerShopId) {
		CustomerShopInfoEntity info = shopInfoService.queryById(customerShopId);
		ViewContext.put("view", info);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/view_info_shop_new");
	}
	
	/**
	 * @Description:
	 *               <p>
	 *               新门店地址 脱敏
	 *               </p>
	 * @author LMX
	 */
	@RequestMapping("/view_address")
	public BaseRestResult<String> view_address(String customerShopId) {
		CustomerShopInfoEntity info = shopInfoService.queryById(customerShopId);
		ViewContext.put("view", info);
		return HtmlRestResult.popHtmlFragment("/nsEditDetail/view_address_shop");
	}
}
